package com.yhh.college_information_platform.controller;

import com.yhh.college_information_platform.annotation.PublicAccess;
import com.yhh.college_information_platform.common.Result;
import com.yhh.college_information_platform.service.CategoryService;
import com.yhh.college_information_platform.vo.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理Controller（所有接口游客可访问）
 *
 * @author yhh
 * @date 2025-10-25
 */
@Slf4j
@RestController
@RequestMapping("/category")
@PublicAccess  // 整个Controller都允许游客访问
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取分类树（所有分类的树形结构）
     */
    @GetMapping("/tree")
    public Result<List<CategoryVO>> getCategoryTree() {
        List<CategoryVO> tree = categoryService.getCategoryTree();
        return Result.success(tree);
    }

    /**
     * 获取一级分类列表
     */
    @GetMapping("/first-level")
    public Result<List<CategoryVO>> getFirstLevelCategories() {
        List<CategoryVO> categories = categoryService.getFirstLevelCategories();
        return Result.success(categories);
    }

    /**
     * 根据父分类ID获取子分类列表
     */
    @GetMapping("/children/{parentId}")
    public Result<List<CategoryVO>> getCategoriesByParentId(@PathVariable Long parentId) {
        List<CategoryVO> categories = categoryService.getCategoriesByParentId(parentId);
        return Result.success(categories);
    }

    /**
     * 根据ID获取分类详情
     */
    @GetMapping("/{id}")
    public Result<CategoryVO> getCategoryById(@PathVariable Long id) {
        CategoryVO category = categoryService.getCategoryById(id);
        return Result.success(category);
    }
}

